
package com.twb.auth.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.twb.auth.entity.twbPost.TwbPost;
import com.twb.auth.entity.twbPost.dto.SearchTwbPostDto;
import com.twb.auth.entity.twbPost.vo.SaveTwbPostVo;
import com.twb.auth.entity.twbPost.vo.SearchTwbPostVo;
import com.twb.auth.entity.twbPost.vo.UpdateTwbPostByIdVo;
import com.twb.auth.mapper.TwbPostMapper;
import com.twb.auth.service.ITwbPostService;
import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.base.service.impl.BaseService;
import com.twb.core.exception.ServiceException;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.ExcelUtils;
import com.twb.core.util.TwbPageUtil;
import com.twb.system.util.ExcelDataDicHandlerImpl;

/**
 * @ClassName: TwbPostService
 * @Description: 岗位信息Service
 * 
 * @author DHL
 * @date 2022-10-24 20:45:59
 */
@Service("twbPostService")
public class TwbPostService extends BaseService<TwbPostMapper, TwbPost> implements ITwbPostService {

	@Autowired
	TwbPostMapper twbPostMapper;

	@Override
	public TwbPageInfo<SearchTwbPostDto> searchTwbPost(SearchTwbPostVo vo) {
		TwbPageUtil.startPage(vo);
		List<SearchTwbPostDto> list = twbPostMapper.searchTwbPost(vo);
		return TwbPageUtil.of(list);
	}

	@Override
	public List<SearchTwbPostDto> searchTwbPostList(SearchTwbPostVo vo) {
		TwbPageUtil.orderBy("post_seq asc");
		return twbPostMapper.searchTwbPost(vo);
	}

	@Override
	public SearchTwbPostDto searchTwbPostSum(SearchTwbPostVo vo) {
		SearchTwbPostDto result = twbPostMapper.searchTwbPostSum(vo);
		return result;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean saveTwbPost(SaveTwbPostVo vo) {
		TwbPost checkVo = new TwbPost();
		// 此处需要修改为需要校验重复的字段
		checkVo.setName(vo.getName());
		checkVo.setIsdel(0);
		long count = this.count(checkVo);
		if (count > 0) {
			throw new ServiceException(String.format("[%s]已存在", checkVo.getName()));
		}

		Integer maxSeq = twbPostMapper.getMaxSeq() + 1;

		TwbPost record = new TwbPost();
		BeanUtils.copyNotNullProperties(vo, record);
		record.setId(this.getUUID());
		record.setPostSeq(maxSeq);
		this.setCreateInfo(record);
		this.setUpdateInfo(record);
		return this.save(record);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateTwbPostById(UpdateTwbPostByIdVo vo) {
		TwbPost record = twbPostMapper.selectById(vo.getId());
		if (null == record) {
			throw new ServiceException("数据不存在");
		}

		if (!vo.getName().equalsIgnoreCase(record.getName())) {
			TwbPost checkVo = new TwbPost();
			// 此处需要修改为需要校验重复的字段
			checkVo.setName(vo.getName());
			checkVo.setIsdel(0);
			long count = this.count(checkVo);
			if (count > 0) {
				throw new ServiceException(String.format("[%s]已存在", checkVo.getName()));
			}
		}

		BeanUtils.copyNotNullProperties(vo, record);
		this.setUpdateInfo(record);
		return this.updateById(record);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteTwbPostByIds(String ids) {
		return this.removeByIds(Arrays.asList(ids.split(",")));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int logicBatchDeleteTwbPostByIds(String ids) {
		List<String> list = new ArrayList<String>();
		Collections.addAll(list, ids.split(","));
		return twbPostMapper.logicBatchDeleteTwbPostByIds(list);
	}

	@Override
	public void exportExcel(SearchTwbPostVo vo, HttpServletResponse response) throws Exception {
		String fileName = vo.getFileName();
		if (StringUtils.isBlank(fileName)) {
			fileName = "岗位信息";
		}
		TwbPageInfo<SearchTwbPostDto> pageInfo = this.searchTwbPost(vo);
		List<SearchTwbPostDto> list = pageInfo.getList();
		ExcelDataDicHandlerImpl excelDataDicHandlerImpl = new ExcelDataDicHandlerImpl();
		ExcelUtils.exportExcel(fileName, list, SearchTwbPostDto.class, response, excelDataDicHandlerImpl);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int importExcel(Map<String, String> map) {
		System.out.println(map.toString());
		return 0;
	}
}
